const { app, BrowserWindow, screen } = require("electron");

const path = require("path");

function createWindow() {
    //获取屏幕分辨率
    const mainScreen = screen.getPrimaryDisplay();
    const { width, height } = mainScreen.size;
    console.log(`w:${width} h:${height}`)
    let { winWidth, winHeight } = 0;
    //计算屏幕分辨率 设置为16 : 10
    winHeight = height * 0.5;
    winWidth = winHeight / 9 * 16;
    const win = new BrowserWindow({
        width: winWidth,
        height: winHeight,
        frame: false,
        // resizable: false,
        icon: path.join(__dirname, "dist/favicon.ico"),
        autoHideMenuBar: true,
        show: false,
    });
    // win.webContents.openDevTools()
    win.loadFile("dist/index.html");


    win.on('ready-to-show', () => {
        win.show() // 初始化后再显示
    })
}

app.whenReady().then(() => {
    createWindow();
    app.on("activate", () => {
        if (BrowserWindow.getAllWindows().length === 0) {
            createWindow();
        }
    });
});

app.on("window-all-closed", () => {
    if (process.platform !== "darwin") {
        app.quit();
    }
});

app.on("browser-window-created", (event, win) => {
    win.webContents.setWindowOpenHandler(({ url }) => {
        console.log("url: ", url)
        //如果.在最后一个/之后 说明是文件 那么就不显示
        if (url.lastIndexOf("/") < url.lastIndexOf(".")) {
            return {
                action: 'allow',
                overrideBrowserWindowOptions: {
                    resizable: false,
                    webPreferences: {
                        preload: path.join(__dirname, "preload.js"),
                    },
                    icon: path.join(__dirname, "dist/favicon.ico"),
                    autoHideMenuBar: true,
                    show: false,
                }
            }
        }
        return {
            action: 'allow',
            overrideBrowserWindowOptions: {
                resizable: false,
                webPreferences: {
                    preload: path.join(__dirname, "preload.js"),
                },
                icon: path.join(__dirname, "dist/favicon.ico"),
                autoHideMenuBar: true,
            }
        }
    })
})